<template>
  <div>
    <el-row class="params">
      <label class="row-lable">订单编号:</label>
      <el-input style="width:160px" v-model="tradeCode"></el-input>
      <label class="row-lable">推荐人id:</label>
      <el-input style="width:160px" v-model="agentId"></el-input>
      <el-button type="primary" @click="jump(1)">搜索</el-button>
      <el-button type="primary" style="float: right;margin-right: 30px" @click="exportNoSend()">导出</el-button>
    </el-row>

    <el-table :data="listData" border highlight-current-row stripe style="width: 100%;margin-top: 20px">
      <el-table-column prop="tradeCode" label="订单号"></el-table-column>
      <el-table-column prop="payTime" label="下单时间"></el-table-column>
      <el-table-column label="买家信息">
        <template slot-scope="scope">
          <template>id:{{scope.row.buyerId}}</template>
          <br>
          <template>昵称:{{scope.row.buyer}}</template>
          <template></template>
        </template>
      </el-table-column>
      <el-table-column label="邀请人信息">
        <template slot-scope="scope">
          <template v-if="scope.row.agent">
            ID:{{scope.row.agent.agentId}}
            <br>
            昵称:{{scope.row.agent.nick}}
            <br>
            职务:{{scope.row.agent.dutyName}}
          </template>
          <el-button type="text" @click="bonusDetail(scope.row.tradeCode)">详情</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="itemCode" label="商品编号"></el-table-column>
      <el-table-column label="下单商品信息">
        <template slot-scope="scope">
          <img :src="scope.row.imgUrl" style="max-height: 60px;max-width: 60px">
          <br>
          <template>{{scope.row.title}}</template>
        </template>
      </el-table-column>
      <el-table-column label="商品档口" prop="storeNum"></el-table-column>
      <el-table-column label="拿货号" prop="goodsNo"></el-table-column>
      <el-table-column label="实付金额">
        <template slot-scope="scope">
          <template>{{scope.row.payMoney}}元</template>
          <br>
          <template>(含运费：{{scope.row.postFee}}元)</template>
        </template>
      </el-table-column>
      <el-table-column label="规格">
        <template slot-scope="scope">
          <template>{{scope.row.sku}}；</template>
          <template>数量{{scope.row.num}}</template>
        </template>
      </el-table-column>
      <el-table-column label="收货信息">
        <template slot-scope="scope">
          <template>姓名:{{scope.row.receiveName}}</template>
          <br>
          <template>电话:{{scope.row.receiveTelephone}}</template>
          <br>
          <template>地址:{{scope.row.provName}}{{scope.row.cityName}}{{scope.row.areaName}}{{scope.row.receiveDetail}}
          </template>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="dialogShowBtn(scope.row)">发货</el-button>
          <br>
          <el-button type="text" size="small" @click="open(scope.row)">关闭交易</el-button>
          <br>
          <el-button type="text" size="small" @click="openRefund(scope.row)">退款</el-button>
          <br>
          <el-button type="text" size="small" @click="openModifyOrderInfoWindow(scope.row)">修改收货信息</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog type="text" title="发货" :modal-append-to-body="false" :visible.sync="dialogVisible" width="30%">
      <el-form>
        <el-form-item label="物流公司">
          <el-select v-model="value" placeholder="请选择">
            <el-option v-for="(item,index) in options" :key="index" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物流单号">
          <el-input v-model="postCode"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" size="medium" v-model="remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
         <el-button @click="dialogVisible = false">取 消</el-button>
         <el-button type="primary" @click="dialogVisibleTrue">确 定</el-button>
       </span>
    </el-dialog>
    <el-dialog title="修改收货信息" :modal-append-to-body="false" :visible.sync="isShowWindow" width="30%">
      <el-form>
        <el-form-item label="姓名:">
          <el-input v-model="orderInfo.receiveName"></el-input>
        </el-form-item>

        <el-form-item label="手机号:">
          <el-input v-model="orderInfo.receiveTelephone"></el-input>
        </el-form-item>

        <el-form-item label="省:">
          <el-input v-model="orderInfo.provName"></el-input>
        </el-form-item>

        <el-form-item label="市:">
          <el-input v-model="orderInfo.cityName"></el-input>
        </el-form-item>

        <el-form-item label="区:">
          <el-input v-model="orderInfo.areaName"></el-input>
        </el-form-item>

        <el-form-item label="地址:">
          <el-input v-model="orderInfo.receiveAddress"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowWindow = false">取 消</el-button>
        <el-button type="primary" @click="confirmNewOrderInfo">确定修改</el-button>
      </span>
    </el-dialog>

    <el-pagination
      background
      @current-change="jump"
      layout="total, prev, pager, next, jumper"
      :pageSize=15
      :total="total">
    </el-pagination>
    <el-dialog title="订单待分配详情" :modal-append-to-body="false" :visible.sync="bonusShow" width="30%">
      <el-form>
        <el-form-item>
          <label>BOSS:{{bonusInfo.boss}}</label><br>
          <label>BOSS奖励: {{bonusInfo.bossBonus}} 元</label>
        </el-form-item>
        <el-form-item>
          <label>店长:{{bonusInfo.owner}}</label><br>
          <label>店长奖励: {{bonusInfo.ownerBonus}} 元</label>
        </el-form-item>
        <el-form-item>
          <label>店员:{{bonusInfo.staff}}</label><br>
          <label>店员奖励: {{bonusInfo.staffBonus}} 元</label>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog type="text" :modal-append-to-body="false" :visible.sync="refundParam.show" width="30%">
      <div>
        <p>执行订单退款?金钱将返回原微信账号</p>
      </div>
      <span slot="footer" class="dialog-footer">
    <el-button @click="refundParam.show = false">取 消</el-button>
    <el-button type="primary" @click="submitRefund">确 定</el-button>
    </span>
    </el-dialog>
    <el-dialog title="退款信息" :modal-append-to-body="false" :visible.sync="refundResult.show" width="30%">
      <el-form>
        <el-form-item label="商户订单号">
          {{refundResult.outTradeNo}}
        </el-form-item>
        <el-form-item label="退款金额">
          {{refundResult.wxNo}}
        </el-form-item>
        <el-form-item label="微信退款id">
          {{refundResult.refundId}}
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import {
    ajax,
    toSendList,
    orderSend,
    closeTrade,
    changeTradeReceive,
    tradeBonus,
    refundTrade,
    queryWxRefund
  } from "../../data/service";

  export default {
    name: "mallOrder",
    data() {
      return {
        options: [
          {
            value: '韵达快递',
            label: '韵达快递'
          },
          {
            value: '中通快递',
            label: '中通快递'
          },
          {
            value: '顺丰快递',
            label: '顺丰快递'
          },
          {
            label: '圆通快递',
            value: '圆通快递'
          },
          {
            label: '中通快递',
            value: '中通快递'
          },
          {
            label: '天天快递',
            value: '天天快递'
          },
          {
            label: 'EMS快递',
            value: 'EMS快递'
          },
          {
            label: '百世快递',
            value: '百世快递'
          },
          {
            label: '邮政包裹',
            value: '邮政包裹'
          },
          {
            label: '其他',
            value: '其他'
          }
        ],
        value: '',
        tradeCode: '',
        SenTradeCode: '',
        listData: [],
        page: 1,
        total: 0,
        dialogVisible: false,
        postCompany: '',
        postCode: '',
        remark: '',
        agentId: '',
        isShowWindow: false,
        orderInfo: {},
        bonusInfo: {},
        bonusShow: false,
        refundParam: {
          tradeCode: '',
          show: false
        },
        refundResult: {
          tradeCode: '',
          outTradeNo:'',
          wxNo:'',
          refundId:'',
          refundFee:'',
          show: false
        }
      }
    },
    mixins: [ajax],
    created() {
      this.getList();
    },
    methods: {
      getList() {
        this.doajax(toSendList,
          {
            page: this.page,
            tradeCode: this.tradeCode === undefined ? "" : this.tradeCode,
            agentId: this.agentId === undefined ? "" : this.agentId
          }, (resp) => {
            this.listData = resp.data;
            this.total = resp.total;
          })
      },
      dialogVisibleTrue() {
        this.doajax(orderSend, {
          tradeCode: this.SenTradeCode,
          postCompany: this.value,
          postCode: this.postCode,
          remark: this.remark
        }, (resp) => {
          this.postCompany = '';
          this.postCode = '';
          this.remark = '';
          this.dialogVisible = false;
          this.getList()
        })
      },

      open(row) {
        this.$confirm('确定关闭吗?', '关闭交易', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(() => {
          let tradeCode = row.tradeCode;
          this.doajax(closeTrade, {tradeCode: tradeCode}, (resp) => {
            this.success("已关闭交易");
            this.getList();
          })
        }).catch(() => {

        });
      },

      openModifyOrderInfoWindow(row) {
        this.orderInfo = {};
        this.isShowWindow = true;
        this.orderInfo = row
        this.orderInfo.receiveAddress = row.receiveDetail
      },

      confirmNewOrderInfo() {
        let data = {}
        for (let key in this.orderInfo) {
          if (key == 'tradeCode' || key == 'provName' || key == 'cityName' || key == 'areaName' || key == 'receiveName' || key == 'receiveTelephone' || key == 'receiveAddress') {
            data[key] = this.orderInfo[key];
          }
        }
        this.doajax(changeTradeReceive, data, resp => {
          this.isShowWindow = false;
          this.getList()
        })
      },

      dialogShowBtn(row) {
        this.SenTradeCode = row.tradeCode;
        this.dialogVisible = true;
      },

      jump(val) {
        this.page = val;
        this.getList();
      },
      exportNoSend() {
        window.location.href = '/adminjson/exportNoSend.json';
      },
      bonusDetail(tradeCode) {
        this.doajax(tradeBonus, {tradeCode: tradeCode}, resp => {
          this.initBonusInfo();
          this.bonusInfo.staff = resp.staff;
          this.bonusInfo.owner = resp.owner;
          this.bonusInfo.boss = resp.boss;
          this.bonusInfo.staffBonus = resp.staffBonus;
          this.bonusInfo.ownerBonus = resp.ownerBonus;
          this.bonusInfo.bossBonus = resp.bossBonus;
          this.bonusShow = true;
        })
      },
      initBonusInfo() {
        this.bonusInfo = {
          staff: '',
          owner: '',
          boss: '',
          staffBonus: '',
          ownerBonus: '',
          bossBonus: ''
        }
      },

      openRefund(row) {
        this.refundParam.tradeCode = row.tradeCode;
        this.refundParam.show = true;
      },
      submitRefund() {
        this.doajax(refundTrade,
          {
            tradeCode: this.refundParam.tradeCode
          },
          resp => {
            this.refundResult.tradeCode = this.refundParam.tradeCode;
            this.refundParam.tradeCode = '';
            this.refundParam.show = false;
            this.getList();
          }
        );
      },
      queryRefundResult() {
        this.doajax(queryWxRefund,
          {
            tradeCode: this.refundResult.tradeCode
          },
          resp => {
            this.refundResult.show = true;
            this.refundResult.refundId = resp.refundId;
            this.refundResult.refundFee = resp.refundFee;
            this.refundResult.outTradeNo = resp.outTradeNo;
            this.refundResult.outRefundNo = resp.outRefundNo;
            this.refundResult.show = true;

          }
        )
      }

    },

  }
</script>

<style scoped>

</style>
